package cn.t.facade.user;


import cn.t.converter.user.CustomerInfoDTOConverter;
import cn.t.core.facade.FacadeImpl;
import cn.t.dto.user.CustomerFirmDTO;
import cn.t.dto.user.CustomerIndividualDTO;
import cn.t.dto.user.CustomerInfoDTO;
import cn.t.core.enums.base.BaseStatusEnum;
import cn.t.enums.user.CustomerTypeEnum;
import cn.t.model.user.CustomerInfoDO;
import cn.t.service.user.CustomerFirmService;
import cn.t.service.user.CustomerInfoService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 客户信息表服务编排
 *
 * @author l
 */
@Slf4j
@Service
@AllArgsConstructor
public class CustomerInfoFacade extends FacadeImpl<CustomerInfoDTO, CustomerInfoDO, CustomerInfoDTOConverter, CustomerInfoService> {



    public CustomerInfoDTO createIndividual(CustomerIndividualDTO customerIndividualDTO) {

        CustomerInfoDTO customerInfoDTO = new CustomerInfoDTO();
        customerInfoDTO.setCustomerType(CustomerTypeEnum.INDIVIDUAL.getCode());
        customerInfoDTO.setStatus(String.valueOf(BaseStatusEnum.AVAILABLE.getCode()));
        customerInfoDTO.setGmtCreated(new Date());

        Long customerId = service.addAndReturnId(converter.dto2Model(customerInfoDTO));
        customerIndividualDTO.setCustomerId(customerId);
        customerIndividualDTO.setId(customerId);
        customerIndividualDTO.setStatus(String.valueOf(BaseStatusEnum.AVAILABLE.getCode()));
        customerIndividualDTO.setGmtCreated(new Date());
        customerIndividualFacade.add(customerIndividualDTO);

        customerInfoDTO.setId(customerId);
        return customerInfoDTO;
    }


    public Long createFirm(CustomerFirmDTO customerFirmDTO) {

        hasDuplicatedFirm(customerFirmDTO);

        CustomerInfoDTO customerInfoDTO = new CustomerInfoDTO();
        customerInfoDTO.setCustomerType(CustomerTypeEnum.FIRM.getCode());
        customerInfoDTO.setStatus(String.valueOf(BaseStatusEnum.AVAILABLE.getCode()));
        customerInfoDTO.setGmtCreated(new Date());

        Long customerId = service.addAndReturnId(converter.dto2Model(customerInfoDTO));
        customerFirmDTO.setCustomerId(customerId);
        customerFirmDTO.setId(customerId);
        customerFirmDTO.setStatus(String.valueOf(BaseStatusEnum.AVAILABLE.getCode()));
        customerFirmDTO.setGmtCreated(new Date());

        customerFirmFacade.add(customerFirmDTO);

        return customerId;
    }

    public void hasDuplicatedFirm(CustomerFirmDTO customerFirmDTO) {
        if (customerFirmDTO == null || StringUtils.isBlank(customerFirmDTO.getFirmCode())) {
            return;
        }
        customerFirmService.isDuplicatedFirm(null, customerFirmDTO.getFirmCode());
    }




    @Autowired
    private CustomerIndividualFacade customerIndividualFacade;

    @Autowired
    private CustomerFirmFacade customerFirmFacade;


    @Autowired
    private CustomerFirmService customerFirmService;

}

